﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using TireService.Data;

namespace TireService.Website
{
    public partial class Gavekort : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void rblGiftCard_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (rblGiftCard.Value.ToString() != "0")
                {
                    txtGiftCardValue.Text = rblGiftCard.Value.ToString();
                }
            }
            catch (Exception)
            {

                // Nothing
            }
        }

        /// <summary>
        /// Button function that sends email to us with request from user to be contacted with tire prices
        /// </summary>
        protected void btnSendRequest_Click(object sender, EventArgs e)
        {
            try
            {
                string emailaddress = Convert.ToString(ConfigurationManager.AppSettings["EmailAddress"]);
                bool test = Convert.ToBoolean(Convert.ToByte(ConfigurationManager.AppSettings["TestEnvironmentOn"]));
                string message = String.Format("Kunde har bestilt gavekort<br><br>Navn: {0}<br>Adresse: {1}, {2}<br>Email: {3}<br>Gavekorttype: {4}<br>Gavekort værdi: {5}<br>Kommentar: {6}", txtName.Text, txtAddress.Text, txtZipCodeCity.Text, txtEmail.Text, rblGiftCard.SelectedItem.Text, txtGiftCardValue.Text, txtComment.Text);

                Shared email = new Shared();
                email.SendEmail(emailaddress, txtEmail.Text, message, "Gavekort bestilt", true, test, null, null, null);

                // Clear form
                txtEmail.Text = "";
                txtName.Text = "";
                txtAddress.Text = "";
                txtZipCodeCity.Text = "";
                txtPhone.Text = "";
                rblGiftCard.SelectedIndex = -1;
                txtGiftCardValue.Text = "";
                txtComment.Text = "";

                // Show message to user
                lMessage.Text = "Tak for din bestilling - vi vil hurtigst muligt sende gavekort og faktura til dig.";
            }
            catch (Exception ex)
            {
                if ((ex) is ArgumentException)
                {
                    // User error - arguments not valid - show error message
                    lMessage.Text = ((ArgumentException)ex).Message;
                }
                else
                {
                    // System error - show general error message
                    lMessage.Text = "Fejl opstod - venligst prøv igen";
                }
            }
            finally
            {
                // Update message panel
                upMessage.Update();
            }
        }
    }
}